<?php

class Application_Form_User extends Zend_Form {

    public function init() {
        
    }

    public function __construct($formAction = "Enregistrer", $legend = null, $auth = null) {

        //Zend_Debug::dump(($this));

        $this->setDecorators(array('FormElements',
            array('HtmlTag', array('tag' => 'div', 'class' => 'row')),
            'Form')); // supprime la balise <dl>

        $decorators = array(
            'ViewHelper',
            'Errors',
            array('Description', array('tag' => 'p', 'class' => 'description')),
            array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'input')), // 3
            array('Label', array('tag' => null, 'class' => '')), // 2
            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'clearfix')) // 1) exterieur
        );

        $noDecorators = array('ViewHelper');

        $id = new Zend_Form_Element_Hidden('id_user');
        $id->setBelongsTo('User')
                ->addFilter('Int')
                ->addFilter('Null');

        $id_cabinet = new Zend_Form_Element_Hidden('id_cabinet');
        $id_cabinet->setBelongsTo('User')
                ->addFilter('Int')
                ->setValue($auth->id_cabinet)
                ->addFilter('Null');


        $title = new Zend_Form_Element_Select('titre');
        $title->setLabel('Civilité')
                ->setBelongsTo('User')
                ->setAttribs(array('class' => 'span2'))
                ->setMultiOptions(array('' => '', 'mr' => 'Monsieur', 'mme' => 'Madame'))
                ->setRequired(true)->addValidator('NotEmpty', true);

        $nom = new Zend_Form_Element_Text('nom');
        $nom->setLabel('Nom')
                ->setBelongsTo('User')
                ->setRequired(true)
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $prenom = new Zend_Form_Element_Text('prenom');
        $prenom->setLabel('Prenom')
                ->setBelongsTo('User')
                ->setRequired(true)
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('eMail')
                ->setBelongsTo('User')
                ->setRequired(true)
                ->addFilter('Null')
                ->addValidator('EmailAddress', true)
                ->setAttrib('class', 'xlarge')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $login = new Zend_Form_Element_Text('login');
        $login->setLabel('Nom d\'utilisateur')
                ->setBelongsTo('User')
                ->setRequired(true)
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $role = new Zend_Form_Element_Select('role');
        $role->setLabel('Rôle')
                ->setBelongsTo('User')
                ->setAttribs(array('class' => 'span4'))
                ->setMultiOptions(array(
                    'user' => 'Utilisateur',
                    'admin' => 'Administrateur'))
                ->setRequired(true)->addValidator('NotEmpty', true);


        // Ajout du mot de passe si Ajout nouveau utilisateur
        if (isset($legend) && $legend == 'Nouveau') {
            $password = new Zend_Form_Element_Password('password');
            $password->setLabel('Mot de passe')
                    ->setBelongsTo('User')
                    ->setRequired(true)
                    ->addFilter('StringTrim')
                    ->addValidator('NotEmpty');
            $this->addElement($password);
        } else {

            // bouton modification du mot de passe
            $urlBack = $this->getView()->url(array('controller' => 'user', 'action' => 'password'), null, true);
            $bnt_passe = new Zend_Form_Element_Button('bnt_passe');
            $bnt_passe->setLabel('Changer le mot de passe')
                    ->setAttrib('class', 'btn info')
                    ->setAttrib('type', 'reset')
                    ->setDecorators(array('ViewHelper',
                        array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'actions', 'style' => 'border-top:0;margin:0;'))
                    ))
                    ->setAttrib('onclick', 'document.location ="' . $urlBack . '"');
            $this->addElement($bnt_passe);
        }


        $urlBack = $this->getView()->url(array('controller' => 'user', 'action' => 'index'), null, true);
        $cancel = new Zend_Form_Element_Button('cancel');
        $cancel->setLabel('Annuler')
                ->setAttrib('class', 'btn')
                ->setAttrib('type', 'reset')
                ->setDecorators(array('ViewHelper'))
                ->setAttrib('onclick', 'document.location ="' . $urlBack . '"');

        $reset = new Zend_Form_Element_Reset('reset');
        $reset->setLabel('Reset')
                ->setAttrib('class', 'btn')
                ->setDecorators(array('ViewHelper'));

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($formAction)
                ->setAttrib('id', 'submitbutton')
                ->setAttrib('class', 'btn primary');

        // Add Elements
        $this->addElements(array($id, $id_cabinet, $title, $nom, $prenom, $email, $login, $role, $cancel, $submit, $reset));

        // Subform Adresse
        $adresseSub = new Application_Form_SubForm_Adresse();

        $field_adresse = null;
        foreach ($adresseSub->getElements() as $name => $element) {

            if ($name != 'fax') {
                $this->addElement($element);
                $field_adresse[] = $name;
            }
        }


        $this->setElementDecorators(array('ViewHelper'), array('submit', 'cancel'));
        $this->setElementDecorators($decorators, array_merge(array('titre', 'prenom', 'nom', 'email', 'login', 'role', 'password'), $field_adresse));
        $this->setElementDecorators($noDecorators, array('id_user', 'id_adresse', 'id_cabinet'));

        // Patient Group
        $this->addDisplayGroup(
                array_merge(array('titre', 'prenom', 'nom', 'email'), $field_adresse)
                , 'userGroup', array('legend' => $legend . " Utilisateur"));

        $contact = $this->getDisplayGroup('userGroup');
        $contact->setDecorators(array(
            'FormElements',
            'Fieldset',
            array('HtmlTag', array('tag' => 'div', 'class' => 'span8')),
            array(array('rowspan' => 'HtmlTag'), array('tag' => 'div', 'openOnly' => true, 'placement' => 'prepend', 'class' => 'row'))));

        // Compte Group
        $this->addDisplayGroup(array('login', 'role', 'password', 'bnt_passe'), 'compteGroup', array('legend' => 'Accés du compte'));

        $this->compteGroup->setDecorators(array(
            'FormElements',
            'Fieldset',
            array('HtmlTag', array('tag' => 'div', 'closeOnly' => true, 'placement' => 'append')),
            array(array('rowspan' => 'HtmlTag'), array('tag' => 'div', 'class' => 'span8'))));

        // Buttons Group
        $this->addDisplayGroup(array('reset','cancel', 'submit' /* , 'bnt_passe' */), 'buttonGroupe');
        $this->buttonGroupe->removeDecorator('Label');
        $this->buttonGroupe->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'div', 'class' => 'actions'))
        ));
    }

}

